package com.example.droidwarehouse;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.droidwarehouse.barcodescanner.IntentIntegrator;
import com.example.droidwarehouse.barcodescanner.IntentResult;
import com.example.droidwarehouse.item.Item;
import com.example.droidwarehouse.item.ItemService;

public class RetrieveActivity extends Activity
{

    private Button b_do_return;
    private Button b_cancel;
    private EditText e_itemID;
    private EditText e_itemName;
    private EditText e_cusName;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
	super.onCreate(savedInstanceState);
	IntentIntegrator.initiateScan(this);			//Scan Code before Activity shows up
	setContentView(R.layout.retrieve_activity);

	e_itemID = (EditText) findViewById(R.id.e_itemID);
	e_itemName = (EditText) findViewById(R.id.e_itemName);
	e_cusName = (EditText) findViewById(R.id.e_cusName);
	b_cancel = (Button) findViewById(R.id.b_cancel);
	b_cancel.setOnClickListener(new OnClickListener()
	{

	    @Override
	    public void onClick(View v)
	    {
		Intent intent = new Intent();
		intent.setClass(RetrieveActivity.this, MainActivity.class);
		startActivity(intent);
		RetrieveActivity.this.finish();
	    }
	});

	b_do_return = (Button) findViewById(R.id.b_do_return);
	b_do_return.setOnClickListener(new OnClickListener()
	{

	    @Override
	    public void onClick(View v)
	    {
		doReturn();
	    }

	});
    }

    private void doReturn()
    {
	// TODO Auto-generated method stub
	Item item = new Item();
	// item.setId();
	// item.setName();

	ItemService service = new ItemService();
	service.retreive(item);

	// back to main activity
	Intent intent = new Intent();
	intent.setClass(RetrieveActivity.this, MainActivity.class);
	startActivity(intent);
	RetrieveActivity.this.finish();
    }

    
    /**
     * Reaction on result of QR Barcode Scanner
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode) {
			case IntentIntegrator.REQUEST_CODE: {
					if (resultCode != RESULT_CANCELED) {
						IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
						if (scanResult != null) {
							String upc = scanResult.getContents();
							e_itemID.setText(upc);
						}
					}
					break;
			}
		}
	}
    
}
